<?php

class TesztKeszites extends Page {

    protected $allowedUsers = array('TEACHER', 'STUDENT');
    protected $template = 'tesztkeszites.tpl';
    protected $kerdesszam;
    protected $tesztnev;
    const DISC_TABLE = 'discipline';
    
    public function __construct() {
        parent::__construct();
        $this->init();
        $action = isset($_POST['save']);
        if ($action == 'save') {
            $this->save();
        }
    }

    public function init() {

        $this->tesztnev = isset($_POST['tesztnev']) ? $_POST['tesztnev'] : null;
        $this->kerdesszam = isset($_POST['kerdesszam']) ? $_POST['kerdesszam'] : null;

        $disc = Dal::fetchAll(" SELECT D.Name AS DName FROM " .
                        self::DISC_TABLE . " D ");

        $this->smarty->assign('disc', $disc);
        $this->smarty->assign('tesztnev', $this->tesztnev);
        $this->smarty->assign('kerdesszam', $this->kerdesszam);
    }

    private function save() {

        $disc = $_POST['disc'];
        $disc = DAL::fetch(" Select Id From discipline Where Name = '{$disc}' ");
        $dId = $disc['Id'];

        $sql = "Insert into test (Name, DisciplineId ) values('$this->tesztnev','$dId')";
        Dal::update($sql);

        $lid = DAL:: fetch(" Select max(Id) From test ");
        $lastid = $lid['max(Id)'];
       

        $j = 1;
        for ($i = 0; $i < $this->kerdesszam; $i++) {
            $kerdes = $_POST['kerdes' . $i];
            $valasz1 = $_POST['valasz' . $j];
            $checked1 = $_POST['Selected' . $j];
            $j++;
            $valasz2 = $_POST['valasz' . $j];
            $checked2 = $_POST['Selected' . $j];
            $j++;
            $valasz3 = $_POST['valasz' . $j];
            $checked3 = $_POST['Selected' . $j];
            $j++;
            $valasz4 = $_POST['valasz' . $j];
            $checked4 = $_POST['Selected' . $j];
            $j++;
            $valasz5 = $_POST['valasz' . $j];
            $checked5 = $_POST['Selected' . $j];
            $j++;

            $sql = "Insert into question (TestId, Kerdesszam, Szoveg, Answer1," .
                    "Answer2, Answer3, Answer4, Answer5, Right1, Right2, Right3, " .
                    "Right4, Right5 ) values('$lastid',1,'$kerdes','$valasz1','$valasz2'," .
                    "'$valasz3','$valasz4','$valasz5','$checked1','$checked2'," .
                    "'$checked3','$checked4','$checked5')";
            DAL::update($sql);
        }
    }

}

?>